package com.controller;

import com.api.CommonResult;
import com.entity.table.DelicaciesTableDef;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.entity.Delicacies;
import com.service.DelicaciesService;

import java.io.Serializable;
import java.util.List;

/**
 * 美食与饮品 控制层。
 *
 * @author admin
 * @since 2024-06-18
 */
@Controller
@RequestMapping("/admin/delicacies")
public class DelicaciesController {

    @Autowired
    private DelicaciesService delicaciesService;
    @GetMapping(value = "/list1")
    public String goodList(ModelMap model, @RequestParam(value = "mImg",defaultValue = "")String mImg,
                           @RequestParam(value = "p",defaultValue = "1") int p,
                           @RequestParam(value = "pageSize",defaultValue = "10")int pageSize) {
        //按照用户名模糊查询
        try {
            QueryWrapper queryWrapper = QueryWrapper.create().where(DelicaciesTableDef.DELICACIES.M_IMG.like(mImg));
            Page<Delicacies> page = new Page<>();
            page.setPageSize(pageSize);//设置每页显示记录数
            page.setPageNumber(p);//设置页码
            Page<Delicacies> pageInfo = delicaciesService.getMapper().paginateWithRelations(page,queryWrapper);
            model.addAttribute("page", pageInfo);
            model.addAttribute("total", pageInfo.getTotalRow());
        } catch (Exception e){
            e.printStackTrace();
        }
        return "back/coffee-list1";
    }
    /**
     * 添加美食与饮品。
     *
     * @param delicacies 美食与饮品
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
//    @PostMapping("save")
//    public boolean save(@RequestBody Delicacies delicacies) {
//        return delicaciesService.save(delicacies);
//    }

    /**
     * 根据主键删除美食与饮品。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public boolean remove(@PathVariable Serializable id) {
        return delicaciesService.removeById(id);
    }

    /**
     * 根据主键更新美食与饮品。
     *
     * @param delicacies 美食与饮品
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("update")
    public boolean update(@RequestBody Delicacies delicacies) {
        return delicaciesService.updateById(delicacies);
    }

    /**
     * 查询所有美食与饮品。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    public List<Delicacies> list() {
        return delicaciesService.list();
    }

    /**
     * 根据美食与饮品主键获取详细信息。
     *
     * @param id 美食与饮品主键
     * @return 美食与饮品详情
     */
    @GetMapping("getInfo/{id}")
    public Delicacies getInfo(@PathVariable Serializable id) {
        return delicaciesService.getById(id);
    }

    /**
     * 分页查询美食与饮品。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public Page<Delicacies> page(Page<Delicacies> page) {
        return delicaciesService.page(page);
    }
    //添加
    @PostMapping("save")
    @ResponseBody
    public CommonResult save(Delicacies delicacies) {
        try {
            boolean r = delicaciesService.save(delicacies);
            return CommonResult.success(r);
        } catch (Exception e){
            e.printStackTrace();
            String msg = delicacies.getMImg()+"已存在";
            return  CommonResult.failed(msg);
        }
    }
    //添加修改商品页面代码
    @GetMapping(value = "updateView")
    public String updateView(int id, Model model){
        List<Delicacies> typeList = delicaciesService.list();
        Delicacies delicacies = delicaciesService.getById(id);
        model.addAttribute("typeList",typeList);
        model.addAttribute("delicacies",delicacies);
        return "back/coffee-list1";
    }
}
